Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 417f14d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
gkachru
force-pushed
the
pr/postgres-bus
branch
from
November 4, 2026 17:40
3e5aa17 to
08e48b1
Compare
Add postgresBusDriver, a bus driver that uses Postgres LISTEN/NOTIFY, exported from bentocache/drivers/postgres. pg and @types/pg are optional peer dependencies. - Opens two connections from one connection string, a listener and a publisher. Either is replaced on its own when it fails. It creates no table or other database object. - Sends each batch in one transaction and one round trip, with synchronous_commit off. Messages published while a transaction runs go in the next one, merged by channel, type and namespace with duplicate keys removed. batchDelay (default 0) trades write latency for fewer transactions. - Splits large messages across notifications and transactions. A key too long for a notification becomes a clear of its namespace and is reported to onError. - An end-to-end heartbeat (every 30s by default) replaces a listener that silently stops receiving notifications. Time the process was frozen doesn't count against it. Its error links to the docs on the usual causes. - Errors go to onError. Without it, they are logged with console.error, each distinct message at most once a minute. - Destroys the socket of a connection a stalled server doesn't close, so it can't keep the process alive. Names its sessions bentocache-bus:listener and :publisher unless an application name is set. - At startup, publishes wait for the first connection. Later, they fail fast while the publisher is down, so the bus keeps them in its retry queue. disconnect() sends pending messages first. - Versioned ASCII wire format whose fields are only appended. Channel names that are too long or not ASCII are mapped to a hash. - Turns off idle_session_timeout on the listener with a plain SET, which poolers such as PgDog keep to that client.
gkachru
force-pushed
the
pr/postgres-bus
branch
from
November 4, 2026 17:58
08e48b1 to
417f14d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
postgresBusDriver, a bus driver that uses PostgresLISTEN/NOTIFY, so apps that already run Postgres can sync their L1 caches without running Redis. It is exported frombentocache/drivers/postgres.pgand@types/pgare optional peer dependencies (pg^8.0.3).Why it lives in BentoCache and not in
@boringnode/busA
NOTIFYpayload is text and must be under 8000 bytes, and everyNOTIFYtransaction costs Postgres a transaction id and a WAL record. To stay within those limits, the transport reads theCacheBusMessageit is given (type, namespace, keys):A generic transport in
@boringnode/busonly sees an opaque payload, so it could do none of this.How it works
synchronous_commitoff. Messages published while a transaction runs go in the next one.batchDelay(default0) trades write latency for fewer transactions.disconnect()sends pending messages first.LISTENbut never deliver, and when a load balancer or NAT silently drops an idle connection. Time the process was frozen doesn't count against it. The error links to a docs section on the usual causes, and the docs say whenheartbeat: falseis safe.onError. Without it, they are logged withconsole.error, each distinct message at most once a minute.bentocache-bus:listenerandbentocache-bus:publisherunless an application name is set.idle_session_timeouton the listener with a plainSET.Docs
multi_tier.mdgets a Postgres bus section: options, what the connection must support (direct connections and session-mode poolers work; transaction-mode poolers don't, with the connection strings to use on Supabase, Neon, DigitalOcean and Azure), how it behaves, the heartbeat, and its effect on the rest of the database.Tests
tests/bus/postgres_bus.spec.ts(42 tests) runs against the Postgres from the repo's compose file. It covers delivery, merging and splitting, reconnects after the server terminates either connection, a TCP proxy that freezes connections without closing them, startup and shutdown, and option validation.tests/bus/postgres_encoding.spec.ts(21 tests) covers the wire format.pg8.0.3, 8.4, 8.6, 8.7, 8.8, 8.11 and 8.23. Below 8.6, importingpgfrom ESM prints a harmless "Cannot find module 'pg-native'" message.tsc,oxlintandoxfmtare clean.I didn't open an issue first. If you'd rather discuss the design before reviewing, I'm happy to.
This PR was written with the help of AI.